package com.clip.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clip.dao.csch.CityDAO;
import com.clip.dao.csch.DepartmentDAO;
import com.clip.dao.csch.DistrictDAO;
import com.clip.dao.csch.ProvinceDAO;
import com.clip.dao.csch.SchoolDAO;
import com.clip.dao.csch.UniversityDAO;
import com.clip.entity.csch.City;
import com.clip.entity.csch.Department;
import com.clip.entity.csch.District;
import com.clip.entity.csch.Highschool;
import com.clip.entity.csch.Juniorschool;
import com.clip.entity.csch.Province;
import com.clip.entity.csch.School;
import com.clip.entity.csch.University;
import com.clip.entity.csch.Vocationalschool;

@Service
@Transactional
public class CSCHService {
	@Autowired
	private ProvinceDAO provinceDAO;
	@Autowired
	private CityDAO cityDAO;
	@Autowired
	private DistrictDAO districtDAO;
	@Autowired
	private SchoolDAO schoolDAO;
	@Autowired
	private DepartmentDAO departmentDAO;
	
	public List<Province> getAllProvinces() {
		return provinceDAO.findAll();
	}

	public List<City> getCitiesByPid(int pid) {
		return this.cityDAO.findByQuery("from City city where city.pid="+pid+" order by city.id");
	}

	public List<District> getDistrictsBypidppid(int pid, int ppid) {
		return this.districtDAO.findByQuery("from District district where district.pid="+pid+" and district.ppid="+ppid+" order by district.id");
	}

	public List getSchoolsBytypeANDpid(String schooltype,int pid) {
		String entityName=getEntityName(schooltype);
		return schoolDAO.findByQuery("from "+entityName+" e where e.pid="+pid+" order by e.id");
	}
	
	private String getEntityName(String schooltype){
		if(schooltype.equals(University.class.getSimpleName().toLowerCase())){
			return University.class.getName();
		}else if(schooltype.equals(Highschool.class.getSimpleName().toLowerCase())){
			return Highschool.class.getName();
		}else if(schooltype.equals(Juniorschool.class.getSimpleName().toLowerCase())){
			return Juniorschool.class.getName();
		}else if(schooltype.equals(Vocationalschool.class.getSimpleName().toLowerCase())){
			return Vocationalschool.class.getName();
		}else{
			return null;
		}
	}

	public List getDepartmentsBypid(int pid) {
		return departmentDAO.findByQuery("from Department d where d.pid="+pid);
	}
	
}
